home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / DEV / I-Z / Lex.cpt / Lex / LEXLIB.π folder / YYLEX.C < prev    next >
Text File  |  1990-03-14  |  4KB  |  189 lines

  1. /*
  2.   HEADER: CUG    nnn.nn;
  3.   TITLE:    LEX - A Lexical Analyser Generator
  4.   VERSION:       1.0 for IBM-PC
  5.   DATE:    Jan 30, 1985
  6.   DESCRIPTION:   A Lexical Analyser Generator. From UNIX
  7.   KEYWORDS:      Lexical Analyser Generator YACC C PREP
  8.   SYSTEM:    IBM-PC and Compatiables
  9.   FILENAME:      YYLEX.C
  10.   WARNINGS:      This program is not for the casual user. It will
  11.     be useful primarily to expert developers.
  12.   CRC:    N/A
  13.   SEE-ALSO:      YACC and PREP
  14.   AUTHORS:       Scott Guthery 11100 leafwood lane Austin, TX 78750
  15.   COMPILERS:     DESMET-C
  16.   REFERENCES:    UNIX Systems Manuals
  17. */
  18. /*
  19.  * Bob Denny 28-Aug-82  Remove reference to stdio.h
  20.  * Remove code to default lexin, change to call to
  21.  * llstin(), generated by lex depending upon setting
  22.  * of "-s" switch.  Eliminates hardwired dependency
  23.  * on standard I/O library.  Moved declaration of
  24.  * lexin to lexgetc().
  25.  *
  26.  * Bob Denny 31-Aug-82  Add call to lexswitch() in
  27.  * the generated file, to switch to the table whose
  28.  * name was given in the "-t" switch (or to "lextab"
  29.  * if "-t" wasn't given).  Removed hardwired setting
  30.  * of _tabp --> "lextab" here. Now handled automagically.
  31.  *
  32.  * Bob Denny 21-Oct-82  Add llinit() function to re-initialize
  33.  * yylex(), making it serially reusable.
  34.  *
  35.  * Initialize _tabp to NULL so lexswitch() to real table happens
  36.  * only once.
  37.  *
  38.  * Bob Denny 15-Apr-83 Move NBPW to LEX.H and make it 32 on VAX native,
  39.  *    else 16.
  40.  * Scott Guthery 20-Nov-83      Adapt for IBM PC & DeSmet C
  41.  */
  42.  
  43. /*
  44.  * yylex for lex tables
  45.  */
  46. #ifdef  THINK_C
  47. #include <stdlib.h>
  48. #endif
  49.  
  50. #include "lex.h"
  51.  
  52. #define ERROR   256     /* yacc's value */
  53.  
  54. tst__b(c, tab)
  55. register int    c;
  56. char    tab[];
  57. {
  58.     return(tab[(c >> 3) & 037] & (1 << (c & 07)) );
  59. }
  60.  
  61. struct  lextab  *_tabp = 0;
  62.  
  63. extern char     *llsave[];      /* Right-context buffer    */
  64. char    llbuf[100];    /* work buffer    */
  65. char    *llp1   = &llbuf[0];    /* pointer to next avail. in token      */
  66. char    *llp2   = &llbuf[0];    /* pointer to end of lookahead    */
  67. char    *llend  = &llbuf[0];    /* pointer to end of token    */
  68. char    *llebuf = &llbuf[sizeof llbuf];
  69. int     lleof;
  70. int     yylval  = 0;
  71. int     yyline  = 0;
  72.  
  73. yylex()
  74. {
  75.     register int c, st;
  76.     int final, l, llk, i;
  77.  
  78.     register struct lextab *lp;
  79.     char *cp;
  80.  
  81.     /*
  82.     * Call llstin() to default lexin to stdin
  83.     * and assign _tabp to "real" table.
  84.     */
  85.     llstin();    /* Initialize yylex() variables */
  86.  
  87. loop:
  88.     llk = 0;
  89.     if (llset())
  90.     return(0);    /* Prevent EOF loop     */
  91.     st = 0;
  92.     final = -1;
  93.     lp = _tabp;
  94.  
  95.     do {
  96.     if (lp->lllook && (l = lp->lllook[st])) {
  97.     for (c=0; c<NBPW; c++)
  98.     if (l&(1<<c))
  99.     llsave[c] = llp1;
  100.     llk++;
  101.     }
  102.     if ((i = lp->llfinal[st]) != -1) {
  103.     final = i;
  104.     llend = llp1;
  105.     }
  106.     if ((c = llinp()) < 0)
  107.     break;
  108.     if ((cp = lp->llbrk) && llk==0 && tst__b(c, cp)) {
  109.     llp1--;
  110.     break;
  111.     }
  112.     } while ((st = (*lp->llmove)(lp, c, st)) != -1);
  113.  
  114.  
  115.     if (llp2 < llp1)
  116.     llp2 = llp1;
  117.     if (final == -1) {
  118.     llend = llp1;
  119.     if (st == 0 && c < 0)
  120.     return(0);
  121.     if ((cp = lp->llill) && tst__b(c, cp)) {
  122.     lexerror("Illegal character: %c (%03o)", c, c);
  123.     goto loop;
  124.     }
  125.     return(ERROR);
  126.     }
  127.     if (c = (final >> 11) & 037)
  128.     llend = llsave[c-1];
  129.     if ((c = (*lp->llactr)(final&03777)) >= 0)
  130.     return(c);
  131.     goto loop;
  132. }
  133.  
  134. llinp()
  135. {
  136.  
  137.     register c;
  138.     register struct lextab *lp;
  139.     register char *cp;
  140.  
  141.     lp = _tabp;
  142.     cp = lp->llign;    /* Ignore class    */
  143.     for (;;) {
  144.     /*
  145.     * Get the next character from the save buffer (if possible)
  146.     * If the save buffer's empty, then return EOF or the next
  147.     * input character.  Ignore the character if it's in the
  148.     * ignore class.
  149.     */
  150.     c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc();
  151.     if (c >= 0) {    /* Got a character?     */
  152.     if (cp && tst__b(c, cp))
  153.     continue;       /* Ignore it    */
  154.     if (llp1 >= llebuf) {   /* No, is there room?   */
  155.     lexerror("Token buffer overflow");
  156.     exit(1);
  157.     }
  158.     *llp1++ = c;    /* Store in token buff  */
  159.     } else
  160.     lleof = 1;    /* Set EOF signal       */
  161.     return(c);
  162.     }
  163. }
  164.  
  165. llset()
  166. /*
  167.  * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  168.  */
  169. {
  170.     register char *lp1, *lp2;
  171.  
  172.     for (lp1 = llbuf, lp2 = llend; lp2 < llp2;)
  173.     *lp1++ = *lp2++;
  174.     llend = llp1 = llbuf;
  175.     llp2 = lp1;
  176.     return(lleof && lp1 == llbuf);
  177. }
  178.  
  179. /*
  180.  * Re-initialize yylex() so that it can be re-used on
  181.  * another file.
  182.  */
  183. llinit()
  184.    {
  185.    llp1 = llp2 = llend = llbuf;
  186.    llebuf = llbuf + sizeof(llbuf);
  187.    lleof = yylval = yyline = 0;
  188.    }
  189.